package jpb.module.service.manager;

import jpb.module.dao.keyvalue.KeyValueDao;
import jpb.module.dao.manager.ManagerDao;
import jpb.module.dao.manager.ManagerOrgDao;
import jpb.module.daomain.manager.Manager;
import jpb.util.DateFormatUtil;
import jpb.util.Identities;
import jpb.util.MD5Coder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by haol on 2017/3/14.
 */
@Service
public class ManagerService {
        @Autowired
        private ManagerDao managerDao;
        @Autowired
        private ManagerOrgDao managerOrgDao;

        @Autowired
        private KeyValueDao keyValueDao;
        public boolean usingInterceptor(int key){
                return keyValueDao.getValue(key)== 0;
        }


        public Manager getByAccount(String account){
                return managerDao.getByAccount(account);
        }

        public void updateLoginTime(String id,Date nowTime){
                Manager manager = new Manager();
                manager.setId(id);
                manager.setLoginTime(nowTime);
                managerDao.updateLoginTime(manager);
        }

        public void add(Manager manager){
                String id = Identities.uuid();
                manager.setId(id);
                manager.setCreateTime(DateFormatUtil.getYMdHm());
                String MD5 = MD5Coder.encodeAccountPwd(manager.getAccount(), manager.getPassword());
                manager.setPassword(MD5);
                managerDao.add(manager);
        }

        public boolean modifyPwd(String  id,String oldPwd,String newPwd){
                Manager manager = managerDao.get(id);
                if (comparePwd(oldPwd, manager)){
                        manager.setId(id);
                        String MD5 = MD5Coder.encodeAccountPwd(manager.getAccount(),newPwd);
                        manager.setPassword(MD5);
                        managerDao.updatePwd(manager);
                        managerDao.updateTime(manager.getId(),DateFormatUtil.getYMdHm());
                        return true;
                }else {
                        return false;
                }

        }
        public boolean comparePwd(String inputOldPwd, Manager manager){
                inputOldPwd = MD5Coder.encodeAccountPwd(manager.getAccount(),inputOldPwd);
                String oldPwd = manager.getPassword();
                if (inputOldPwd.equals(oldPwd)){
                        return true;
                }else {
                        return false;
                }
        }
        public void updateHead(Manager manager){
                managerDao.updateHead(manager);
                managerDao.updateTime(manager.getId(),DateFormatUtil.getYMdHm());
        }
        public void update(Manager manager){
                managerDao.updateTime(manager.getId(), DateFormatUtil.getYMdHm());
                managerDao.updateInfo(manager);
        }

        public Manager get(String mid) {
                return managerDao.get(mid);
        }

//        public Manager query(String oid,String mid){
//                return managerDao.getByOidMid(oid,mid);
//        }


        public List<Manager> getByOid(String oid) {
                return managerDao.getByOid(oid);
        }
        public Manager getFounder(String oid){
                return managerDao.getFounder(oid);
        }

        public boolean checkManager(String mid, String oid) {
                return managerOrgDao.get(mid, oid,0) != 0 || this.checkFounder(mid,oid);
        }
        public boolean checkFounder(String mid, String oid) {
                return managerOrgDao.get(mid, oid,1) != 0;
        }

        public void deleteOrgMan(String oid,String mid){
                managerOrgDao.delete(mid,oid);
        }
        public void addOrgMan(String mid, String oid,Integer founder) {
                managerOrgDao.add(mid, oid,founder);
        }





}
